package com.ygqh.baby.service.impl;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import  org.springframework.beans.factory.annotation.Autowired;
import  org.springframework.stereotype.Service;
import  org.springframework.util.Assert;

import  com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.MsgType;
import  com.ygqh.baby.ao.QueryInfo;
import  com.ygqh.baby.ao.ResultSet;
import  com.ygqh.baby.mapper.WxCustomMsgDetailMapper;
import  com.ygqh.baby.po.WxCustomMsgDetail;
import  com.ygqh.baby.po.WxCustomMsgDetailExample;
import  com.ygqh.baby.service.WxCustomMsgDetailService;
import com.ygqh.baby.utils.DateConvertUtils;

@Service
public class WxCustomMsgDetailServiceImpl implements WxCustomMsgDetailService{
	
	@Autowired
	private WxCustomMsgDetailMapper wxCustomMsgDetailMapper;
	
	@Override
	public List<WxCustomMsgDetail> find() {
		WxCustomMsgDetailExample example = new WxCustomMsgDetailExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return wxCustomMsgDetailMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		WxCustomMsgDetailExample example = new WxCustomMsgDetailExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return wxCustomMsgDetailMapper.countByExample(example);
	}

	@Override
	public WxCustomMsgDetail findById(Long id) {
		return wxCustomMsgDetailMapper.selectByPrimaryKey(id);
	}


	

	@Override
	public int save(WxCustomMsgDetail wxCustomMsgDetail) {
		return wxCustomMsgDetailMapper.insertSelective(wxCustomMsgDetail);
	}

	@Override
	public int update(WxCustomMsgDetail wxCustomMsgDetail) {
		return wxCustomMsgDetailMapper.updateByPrimaryKeySelective(wxCustomMsgDetail);
	}

	@Override
	public ResultSet<WxCustomMsgDetail> search(QueryInfo queryInfo,String q, Date startDate, Date endDate) {
		List<WxCustomMsgDetail>  list =  wxCustomMsgDetailMapper.selectSuper(queryInfo, q, startDate, endDate);
		int count = wxCustomMsgDetailMapper.countSuper(q, startDate, endDate);
		ResultSet<WxCustomMsgDetail> resultSet = new ResultSet<WxCustomMsgDetail>(count,list);
		return resultSet;
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		WxCustomMsgDetailExample example = new WxCustomMsgDetailExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		WxCustomMsgDetail record = new WxCustomMsgDetail();
		record.setStatus(DataStatus.Delete);

		return wxCustomMsgDetailMapper.updateByExampleSelective(record, example);
	}
	
	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[]{id});
	}

	@Override
	public int saveOrUpdate(WxCustomMsgDetail wxCustomMsgDetail) {
		int r = 0;
		if(wxCustomMsgDetail.getId() != null) {
			r = this.update(wxCustomMsgDetail);
		}else {
			r = this.save(wxCustomMsgDetail);
		}
		return r;
	}
	@Override
	public void updateStatus(Date sendDate) {
		 wxCustomMsgDetailMapper.updateStatus(sendDate);
	}

	@Override
	public List<WxCustomMsgDetail> findBySendDateAndType(Date date, MsgType msgType) {
		if (date == null) {
			return new ArrayList<>();
		}
		WxCustomMsgDetailExample example = new WxCustomMsgDetailExample();
		WxCustomMsgDetailExample.Criteria criteria = example.createCriteria();
		if (msgType != null) {
			criteria.andMsgTypeEqualTo(msgType.name());
		}
		criteria.andSendDateBetween(DateConvertUtils.getDateStart(date), DateConvertUtils.getDateEnd(date))
			.andStatusEqualTo(DataStatus.Valid.name());
		return wxCustomMsgDetailMapper.selectByExample(example);
	}

}

